<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 17-3-24
 * Time: 下午5:06
 */

namespace common\repositories\myhtsq;

use common\repositories\BaseRepository;
use yii\helpers\ArrayHelper;
use linslin\yii2\curl;;
use Yii;

class ProjectRepository extends BaseRepository
{
    /**
     * 更新项目的授权状态
     * @param       $orgCode
     * @param array $projectId
     * @return bool
     * @throws \Exception
     */
    public function updateProjectAuthStatus($orgCode, array $projectId)
    {
        $curl = new curl\Curl();

        $appCfg = ArrayHelper::getValue(Yii::$app->params, "QmyxRefactorInterface", null);

        if (empty($appCfg)) {
            throw new \Exception("找不到获取已授权数的远程接口配置");
        }

        $appsecret = $appCfg['appSecret'];
        $nowtimestamp = time();
        $sign = md5($nowtimestamp . "#" . $appsecret);
        $siteUrl = rtrim($appCfg['url'], '/') . '?r=project/update-project-auth-status';
        $param = [
            'appid'     => $appCfg["appId"],
            't'         => $nowtimestamp,
            'sign'      => $sign,
            'orgcode'   => $orgCode,
            'projectId' => $projectId,
        ];

        $curl->setOption(CURLOPT_POSTFIELDS, http_build_query($param));

        if ($res = $curl->post($siteUrl) === false) {
            return self::setAndReturn(__LINE__, '请求更新项目的授权状态出错');
        }

        $res = json_decode($res);

        if ($res['retCode'] == 0) {
            return $res['data'];
        }

        return false;
    }


}